package com.abu.crms.controller;


import cn.hutool.core.util.StrUtil;
import com.abu.crms.config.common.Resp;
import com.abu.crms.pojo.ApplyReport;
import com.abu.crms.service.ApplyReportService;
import com.abu.crms.utils.PageUtils;
import com.abu.crms.vo.QueryApplyReportVo;
import com.abu.crms.vo.QueryConditionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 申请记录表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/crms/apply")
public class ApplyReportController {

    @Autowired
    private ApplyReportService applyReportService;

    @GetMapping("applylist")
    public Resp getAllApplicationsByCondition(QueryApplyReportVo queryApplyReportVo) {
        HashMap<String, Object> mapResult = this.applyReportService.queryApplyByPage(queryApplyReportVo);
        return Resp.ok().data(mapResult);
    }

    @GetMapping("/one/{id}")
    public Resp getApplyReportByApplyId(@PathVariable("id") Long applyId) {
        ApplyReport applyReport = this.applyReportService.getById(applyId);
        return Resp.ok().data("applyReport", applyReport);
    }

    @GetMapping("/count")
    public Resp queryNonInspectApplyReport(@RequestParam("reviewerId") Long reviewerId,
                                           @RequestParam(value = "status") Integer status) {
        Integer count = this.applyReportService.queryNonInspectApplyReport(reviewerId, status);
        return Resp.ok().data("count", count);
    }


    @DeleteMapping("apply-id/{id}")
    public Resp deleteByApplyId(@PathVariable("id") Long applyId) {
        boolean flag = this.applyReportService.removeById(applyId);
        if (flag) {
            return Resp.ok().msg("删除成功");
        }
        return Resp.error().msg("删除失败");
    }

    @PutMapping("apply-id/{id}")
    public Resp updateApplyState(@PathVariable("id") Long applyId, @RequestParam("status") Integer status) {
        boolean flag = this.applyReportService.updateState(applyId, status);
        if (flag) {
            return Resp.ok().msg("操作成功");
        }
        return Resp.error().msg("操作失败");
    }

    @DeleteMapping("apply-ids")
    public Resp deleteByApplyId(String idStr) {
//        idStr="1,2,3"
        String[] idArray = StrUtil.split(idStr, ",");
        List<String> idList = Arrays.asList(idArray);
        boolean flag = this.applyReportService.removeByIds(idList);
        if (flag) {
            return Resp.ok().msg("删除成功");
        }
        return Resp.error().msg("删除失败");
    }
}

